package mms

class RestaurantCompanyController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.sort = "id"
        params.order ="desc"

        if (params.keyword) {
          
                 params.keyword = params.keyword.decodeURL()
            
              
             [restaurantCompanyInstanceList: RestaurantCompany.findAllByNameIlike("%${params.keyword}%" ,params), 
                restaurantCompanyInstanceTotal: RestaurantCompany.countByNameIlike("%${params.keyword}%") ,
                keyword : params.keyword]
        }else{
                [restaurantCompanyInstanceList: RestaurantCompany.list(params), 
                restaurantCompanyInstanceTotal: RestaurantCompany.count()]
        }
    }

    def realdata = {
        //println "==========================${params.id}"
        def restaurantCompanyInstance = RestaurantCompany.get(params.id)
        if (restaurantCompanyInstance) {
            params.order = "desc"
            params.sort = "dateCreated"
            params.max = 10;
            [foodRetailInstanceList:FoodRetail.findAllByEnterprise(restaurantCompanyInstance, params),
                        foodRetailInstanceTotal: FoodRetail.countByEnterprise(restaurantCompanyInstance) ,
                        id: params.id]

        }else {
              render("暂时没有数据！") 
        }
    }

    def movie = {
        render("暂时没有数据！")
    }


    def create = {
        def restaurantCompanyInstance = new RestaurantCompany()
        restaurantCompanyInstance.properties = params
        return [restaurantCompanyInstance: restaurantCompanyInstance]
    }

    def save = {
        def restaurantCompanyInstance = new RestaurantCompany(params)
        if (restaurantCompanyInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'restaurantCompany.label', default: 'RestaurantCompany'), restaurantCompanyInstance])}"
            redirect(action: "editgis", id: restaurantCompanyInstance.id)
        }
        else {
            render(view: "create", model: [restaurantCompanyInstance: restaurantCompanyInstance])
        }
    }

    def show = {
        def restaurantCompanyInstance = RestaurantCompany.get(params.id)
        if (!restaurantCompanyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'restaurantCompany.label', default: 'RestaurantCompany'), params.id])}"
            redirect(action: "list")
        }
        else {
            [restaurantCompanyInstance: restaurantCompanyInstance]
        }
    }


    def editgis = {
        def restaurantCompanyInstance = RestaurantCompany.get(params.id)
        if (!restaurantCompanyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'restaurantCompanyInstance.label', default: 'RestaurantCompanyInstance'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [restaurantCompanyInstance: restaurantCompanyInstance]
        }
    }



    def edit = {
        def restaurantCompanyInstance = RestaurantCompany.get(params.id)
        if (!restaurantCompanyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'restaurantCompany.label', default: 'RestaurantCompany'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [restaurantCompanyInstance: restaurantCompanyInstance]
        }
    }

    def update = {
        def restaurantCompanyInstance = RestaurantCompany.get(params.id)
        if (restaurantCompanyInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (restaurantCompanyInstance.version > version) {
                    
                    restaurantCompanyInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'restaurantCompany.label', default: 'RestaurantCompany')] as Object[], "Another user has updated this RestaurantCompany while you were editing")
                    render(view: "edit", model: [restaurantCompanyInstance: restaurantCompanyInstance])
                    return
                }
            }
            restaurantCompanyInstance.properties = params
            if (!restaurantCompanyInstance.hasErrors() && restaurantCompanyInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'restaurantCompany.label', default: 'RestaurantCompany'), restaurantCompanyInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [restaurantCompanyInstance: restaurantCompanyInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'restaurantCompany.label', default: 'RestaurantCompany'), params.id])}"
            redirect(action: "list")
        }
    }

    // 删除多媒体内容
    def deleteMultimedia = {
        def restaurantCompanyInstance = RestaurantCompany.get(params.id)
        Multimedia multimediaInstance = Multimedia.get(params.mid) 

        if (restaurantCompanyInstance) { 
//            medicineCompanyInstance?.multimedia?.toList().each {
//                if (it && it  == multimediaInstance) {
//                    medicineCompanyInstance?.multimedia?.remove(it) 
//                }
//            }  
            restaurantCompanyInstance?.removeFromMultimedia(multimediaInstance);
            restaurantCompanyInstance.save(flush:true)


            multimediaInstance = Multimedia.get(params.mid) 
            multimediaInstance.delete(flush:true)

            return redirect(action: "editgis", id: params.id)
        }
        return redirect(action: "list")
    }


    def delete = {
        def restaurantCompanyInstance = RestaurantCompany.get(params.id)
        if (restaurantCompanyInstance) {
            try {
                restaurantCompanyInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'restaurantCompany.label', default: 'RestaurantCompany'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'restaurantCompany.label', default: 'RestaurantCompany'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'restaurantCompany.label', default: 'RestaurantCompany'), params.id])}"
            redirect(action: "list")
        }
    }
}
